package org.agile4j.patterns.proxy.hr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 代理对象, 代理用户数据对象
 * @author hanyx
 *
 */
public class Proxy implements UserModelApi {

	private static final String DBDRIVER = "com.ibm.db2.jcc.DB2Driver" ;
	private static final String DBURL = "jdbc:db2://192.168.137.225:50000/PC" ;
	private static final String DBUSER = "administrator" ;
	private static final String PASSWORD = "neusoft" ;
	
	/**
	 * 持有被代理的具体的目标对象
	 */
	private UserModel realSubject = null ;
	
	/**
	 * 表示是否已经重新装载过数据
	 */
	private boolean loaded = false ;
	
	/**
	 * 构造方法, 传入被代理的具体的目标对象
	 * @param realSubject 被代理的具体的目标对象
	 */
	public Proxy(UserModel realSubject){
		this.realSubject = realSubject ;
	}
	
	
	@Override
	public String getUserId() {
		return this.realSubject.getUserId();
	}

	@Override
	public void setUserId(String userId) {
		this.realSubject.setUserId(userId) ;
	}

	@Override
	public String getName() {
		return this.realSubject.getName();
	}

	@Override
	public void setName(String name) {
		this.realSubject.setName(name);
	}

	@Override
	public String getDeptId() {
		// 需要判断是否已经装载过了
		if(!this.loaded){
			// 从数据库中重新装载
			reload() ;
			// 设置重新装载的标志位true
			this.loaded = true ;
		}
		return this.realSubject.getDeptId();
	}

	@Override
	public void setDeptId(String deptId) {
		this.realSubject.setDeptId(deptId);
	}

	@Override
	public String getSex() {
		// 需要判断是否已经装载过了
		if (!this.loaded) {
			// 从数据库中重新装载
			reload();
			// 设置重新装载的标志位true
			this.loaded = true;
		}
		return this.realSubject.getSex();
	}

	@Override
	public void setSex(String sex) {
		this.realSubject.setSex(sex);
	}

	/**
	 * 重新查询数据库以获取完整的用户数据
	 */
	private void reload(){
		System.out.println("重新查询数据库获取完整的用户数据, userId==" + realSubject.getUserId());
		Connection conn = null ;
		try{
			conn = this.getConnection() ;
			String sql = "SELECT t1.deptid,t1.sex FROM tb_user t1, tb_dept t2 WHERE t1.deptid = t2.deptid and t1.userid like ? " ;
			PreparedStatement pstmt = conn.prepareStatement(sql) ;
			pstmt.setString(1, realSubject.getUserId());
			ResultSet rs = pstmt.executeQuery() ;
			while(rs.next()){
				// 只需要重新获取userId和name以外的数据
				realSubject.setDeptId(rs.getString(1));
				realSubject.setSex(rs.getString(2));
			}
			rs.close();
			pstmt.close();
		} catch(Exception e){
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	@Override
	public String toString() {
		return "Proxy [getUserId()=" + getUserId() + ", getName()=" + getName() + ", getDeptId()=" + getDeptId() + ", getSex()=" + getSex() + "]" + "\n";
	}


	/**
	 * 取得数据库连接
	 * @return
	 * @throws Exception
	 */
	private Connection getConnection() throws Exception {
		Class.forName(DBDRIVER) ;
		Connection con = DriverManager.getConnection(DBURL, DBUSER, PASSWORD);
		return con;
	}
	
	public static void main(String[] args) throws Exception {
		Proxy p = new Proxy(new UserModel()) ;
		p.reload();
	}
}
